---
title: "Replication: Partisan Communication in Two-Stage Elections"
author: "Marius Saeltzer and Mike Cowburn"
date: "10 8 2023"
output: html_document
---


```{r}
R.Version()
```


### Original Replication System

$platform
[1] "x86_64-w64-mingw32"

$arch
[1] "x86_64"

$os
[1] "mingw32"

$system
[1] "x86_64, mingw32"

$status
[1] ""

$major
[1] "4"

$minor
[1] "0.2"

$year
[1] "2020"

$month
[1] "06"

$day
[1] "22"

$`svn rev`
[1] "78730"

$language
[1] "R"

$version.string
[1] "R version 4.0.2 (2020-06-22)"

$nickname
[1] "Taking Off Again"



```{r}
rm(list = ls()) # yes I know but your computer will thank me.
gc()

```
 

Create the directory. You will find the figures and tables according to their name in the paper in the respective subfolder.

```{r}

dir.create("appendix")
dir.create("appendix/figures")
dir.create("appendix/tables")
dir.create("models_new")

dir.create("bootstrap")
dir.create("figures")
dir.create("tables")

```


Welcome to the Rerpoduction files. We follow the steps of the analysis. We start with 3 datasets:

The tweets we use in the main analysis

A dataset of tweets using a larger set of senators and members of congress for validation.

The expert scores by ...

All else will be built in the process.


### Preprocessing and Training

We use the Twitter corpus we collected during the elections in 2020 (see data/tweets), using the Academic Twitter API. This step, due to the closing of the API, can not be replicated. We hope the users will bear with the dataset we collected.  

Please mind the following: the step of prediciting policy is not easily replicated on a regular laptop computer, as it uses BERT. We ran it on a COLAB with GPUU, using the python script in 0_classify using the dataset usdata.csv as training data. It then predicts the "testtweets.rda" set as csv.

```{r}
set.seed(12345)
```


```{r ,warning=FALSE}
## Build Corpora. Depending your OS, you might have trouble with file encoding. Please ignore warnings.
rm(list = ls())
gc()

if(!file.exists("data/us_dfmn.rdata")){}
suppressMessages(source("scripts_new/1_Preprocessing.R"))
```


After we built our corpora, we train the naive bayes models.




```{r}
rm(list = ls())
gc()
source("scripts_new/2_a_Train_Models.R")
```



```{r}
rm(list = ls())
gc()
source("scripts_new/2_b_Prepare_Datasets.R")
```

Next, we will build ideal points based on friends, following Barbera 2015.

```{r}
rm(list = ls())
gc()
source("scripts_new/2_c_Compute_Tweetscores.R")
```


### Validation


```{r}
rm(list = ls())
gc()
source("scripts_new/3_a_Validation_Partylevel.R")
```


```{r}
rm(list = ls())
gc()
source("scripts_new/3_b_Validation_Individual.R")
```


```{r}
rm(list = ls())
gc()
source("scripts_new/3_c_Validation_Tweetscores.R")
```


### Analysis





Main Analysis + Subanalyses on the party level.

```{r}
rm(list = ls())
gc()
source("scripts_new/4_Main_Analysis.R")

```

```{r}
source("tools/toolbox.R")
getwd()
```


Robustness Checks on the Individual Level.

```{r}

source("scripts_new/5_Robustness_Individual.R")

```


```{r}
zp1<-mult_plot(allModelFrame,level_order=level_order1,sz=14,l=1.5,legend = T)
zp1
pdf('./figures/figure4.pdf',width = 12, height = 10)
zp1
dev.off()

```


Placebo Test for the Appendix

```{r}

rm(list = ls())
gc()
source("scripts_new/6_Placebo_Test.R")

```


```{r}
library(devtools) # before this you may need to install devtools
install_github("hemken/Statamarkdown")


library(Statamarkdown)

```




```{r}

load("./data/main_analysis.rdata")


me$position<-log(me$theta)
me$position_policy<-log(me$theta_r)

me$position_20<-me$position*20
me$position_policy<-me$position_policy*20

me$panel<-NA
me$panel<-ifelse(me$party=="R" & me$winner==0,0,me$panel)
me$panel<-ifelse(me$party=="R" & me$winner==1,1,me$panel)
me$panel<-ifelse(me$party=="D" & me$winner==0,10,me$panel)
me$panel<-ifelse(me$party=="D" & me$winner==1,11,me$panel)

me$democratic<-ifelse(me$party=="D",1,0)

minweek<-min(me$ttp[me$party=="R"])
me2<-me[me$ttp>=minweek,]

haven::write_dta(me2,"fuller.dta")

```


```{stata}

use "fuller", clear
tsset panel ttp

***Main Analysis***

// Dickey Fuller on DVs
program my_results
tsset panel ttp
    dfuller position_20, trend lags(1)
    gen N = `r(N)' 
    gen Zt=`r(Zt)'
    gen p=`r(p)'
    gen cv1=`r(cv1)'
    gen cv5= `r(cv5)'
    gen cv10= `r(cv10)'
end

runby my_results, by(panel) status
contract panel Zt N cv1 cv5 cv10 p
drop _freq
xpose, clear
rename v1 Republican_Loser
rename v2 Republican_Winner
rename v3 Democratic_Loser
rename v4 Democratic_Winner
drop in 1/2
texsave * using ./appendix/tables/table10.tex, replace

***Policy Only****

// Dickey Fuller on DVs
use "fuller", clear
tsset panel ttp
program drop my_results
program my_results
tsset panel ttp
    dfuller position_policy, trend lags(1)
    gen N = `r(N)' 
    gen Zt=`r(Zt)'
    gen p=`r(p)'
    gen cv1=`r(cv1)'
    gen cv5= `r(cv5)'
    gen cv10= `r(cv10)'
end

runby my_results, by(panel) status
contract panel Zt N cv1 cv5 cv10 p
drop _freq
xpose, clear
rename v1 Republican_Loser
rename v2 Republican_Winner
rename v3 Democratic_Loser
rename v4 Democratic_Winner
drop in 1/2
texsave * using ./appendix/tables/table11.tex, replace

// Dickey-Fuller on Residuals
use "fuller", clear
tsset panel ttp
itsa position_20 if democratic ==1, trperiod(0) treatid(10) posttrend
predict u_dem, residuals

drop _position_20 _t _z _z_t _x0 _x_t0 _z_x0 _z_x_t0 _m__position_20_pred
itsa position_20 if democratic ==0, trperiod(0) treatid(0) posttrend
predict u_rep, residuals

gen residuals = u_dem
replace residuals = u_rep if residuals == .

program drop my_results
program my_results
tsset panel ttp
    dfuller residuals, trend lags(1)
    gen N = `r(N)' 
    gen Zt=`r(Zt)'
    gen p=`r(p)'
    gen cv1=`r(cv1)'
    gen cv5= `r(cv5)'
    gen cv10= `r(cv10)'
end

runby my_results, by(panel) status
contract panel Zt N cv1 cv5 cv10 p
drop _freq
xpose, clear
rename v1 Republican_Loser
rename v2 Republican_Winner
rename v3 Democratic_Loser
rename v4 Democratic_Winner
drop in 1/2
texsave * using ./appendix/tables/table12.tex, replace

// Dickey-Fuller on Policy Residuals
use "fuller", clear
tsset panel ttp
itsa position_policy if democratic ==1, trperiod(0) treatid(10) posttrend
predict u_dem_policy, residuals

drop _position_policy _t _z _z_t _x0 _x_t0 _z_x0 _z_x_t0 _m__position_policy
itsa position_policy if democratic ==0, trperiod(0) treatid(0) posttrend
predict u_rep_policy, residuals

gen policy_residuals = u_dem
replace policy_residuals = u_rep if policy_residuals == .

program drop my_results
program my_results
tsset panel ttp
    dfuller policy_residuals, trend lags(1)
    gen N = `r(N)' 
    gen Zt=`r(Zt)'
    gen p=`r(p)'
    gen cv1=`r(cv1)'
    gen cv5= `r(cv5)'
    gen cv10= `r(cv10)'
end

runby my_results, by(panel) status
contract panel Zt N cv1 cv5 cv10 p
drop _freq
xpose, clear
rename v1 Republican_Loser
rename v2 Republican_Winner
rename v3 Democratic_Loser
rename v4 Democratic_Winner
drop in 1/2
texsave * using ./appendix/tables/table13.tex, replace
```

```{stata}

***Policy Only****

// Dickey Fuller on DVs
use "Updated Party Data", clear
tsset panel ttp
program drop my_results
program my_results
tsset panel ttp
    dfuller position_policy, trend lags(1)
    gen N = `r(N)' 
    gen Zt=`r(Zt)'
    gen p=`r(p)'
    gen cv1=`r(cv1)'
    gen cv5= `r(cv5)'
    gen cv10= `r(cv10)'
end

runby my_results, by(panel) status
contract panel Zt N cv1 cv5 cv10 p
drop _freq
xpose, clear
rename v1 Republican_Loser
rename v2 Republican_Winner
rename v3 Democratic_Loser
rename v4 Democratic_Winner
drop in 1/2
texsave * using PolicyResults.tex, replace
```

